package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.aop.Aopannotation;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 购物车
 */
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    @PostMapping("/add")
    @Aopannotation(type = "添加购物车")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        log.info("购物车数据:{}", shoppingCart);

        //设置用户id，指定当前是哪个用户的购物车数据
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //获取菜品的id  如果id!=null,ze添加的是菜品
        Long dishId = shoppingCart.getDishId();

        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());

        if (dishId != null) {
            //购物车添加的是菜品
            lqw.eq(ShoppingCart::getDishId, dishId);
        } else {
            //添加的是套餐
            lqw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }

        //查询当前菜品或者套餐是否在购物车中
        //select *  from shopping_cart where user_id='xxxx' and dish_id='disID'
        //select *  from shopping_cart where user_id='xxxx' and setmeal_id='setmealid'
        ShoppingCart one = shoppingCartService.getOne(lqw);
        if (one != null) {
            //已经存在,在原来数量上加1
            Integer number = one.getNumber();
            one.setNumber(number + 1);
            shoppingCartService.updateById(one);
        } else {
            //不存在 加入设置默认值为1
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            one = shoppingCart;
        }
        return R.success(one);
    }

    //查看购物车
    @GetMapping("/list")
    public R<List<ShoppingCart>> list() {
        log.info("查看购物车");
        //SELECT * FROM shopping_cart WHERE user_id='1599598913036627970' ORDER BY create_time;
        // 条件构造器
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId()); //WHERE user_id='1599598913036627970'
        lqw.orderByDesc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(lqw);
        return R.success(list);
    }


    //*_清空购物车
    //DELETE  http://localhost:8080/shoppingCart/clean
    //DELETE  FROM shopping_cart  WHERE user_id=''
    @DeleteMapping("/clean")
    @Aopannotation(type = "清空购物车")
    public R<String> clean() {
        //DELETE  FROM shopping_cart  WHERE user_id=''
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId()); //WHERE user_id='1599598913036627970'
        shoppingCartService.remove(lqw);
        return R.success("清空购物车成功");
    }
}